#pragma once

#include <iostream>
#include <fstream>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <fcntl.h>
#include <signal.h>
#include <unistd.h>
#include <sys/ioctl.h>  
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/wait.h>
#include <list>

#define BUFLEN          1024
#define SERVER_PORT     5000
#define MAXFILE         3
#define DAMEONPID       "/tmp/dameon.pid"

#define HTTP_VER        "1.0"
#define HTTP_CONF_PATH  "/etc/httpd.conf"
#define CRLF            "\x0d\x0a"

#define HTTP_AGAIN              -2
#define HTTP_ERROR              -1
#define HTTP_TRUE               0
#define HTTP_OK                 0

#define HTTP_MAX_PROCESSES      4089

#define HTTP_CMD_OPEN_CHANNEL   1
#define HTTP_CMD_CLOSE_CHANNEL  2
#define HTTP_CMD_QUIT           3
#define HTTP_CMD_TERMINATE      4
#define HTTP_CMD_REOPEN         5

#define HTTP_PROCESS_SINGLE     0
#define HTTP_PROCESS_MASTER     1
#define HTTP_PROCESS_SIGNALLER  2
#define HTTP_PROCESS_WORKER     3
#define HTTP_PROCESS_HELPER     4

#include "standard/MyPool.h"
#include "snetwork/NluConnection.h"
#include "snetwork/NluEvent.h"

typedef struct http_cycle_s http_cycle_t;
typedef struct http_channel_s http_channel_t;
typedef unsigned int u_int;

/**
 * 全局循环

 */
typedef struct {
    nlu_pool_t                *pool;
    
    nlu_connection_t        **files;
    u_int                   files_n;
    nlu_connection_t         *free_connections;
    u_int                   free_connection_n;
    
    u_int                    connection_n;// how many process
    nl_connection_t          *connections;//connections
    nl_event_t               *read_events;
    nl_event_t               *write_events;
} nlu_cycle_t;


// for socketpair sendmsg/recvmsg
struct http_channel_s{
     unsigned int  command;
     pid_t   pid;
     int   slot;
     int   fd;
};

// all son process infomation , 
typedef struct {
    pid_t               pid;   // process id
    int                 status;  // process status
    int                 channel[2];  // process send|recv msg id

    void                *data;
    char                *name;

    unsigned            respawn:1;
    unsigned            just_spawn:1;
    unsigned            detached:1;
    unsigned            exiting:1;
    unsigned            exited:1;
} http_process_t;

//for signal
typedef struct {
    int     signo;
    const char   *signame;
    const char   *name;
    void  (*handler)(int signo);
} http_signal_t;


extern unsigned int     http_show_help;
extern unsigned int     http_show_version;
extern unsigned int     http_show_configure;
extern unsigned int     http_test_config;
extern unsigned int     http_quiet_mode;
extern unsigned int     http_normal_mode;

extern unsigned char    *http_prefix;
extern unsigned char    *http_conf_file;
extern unsigned char    *http_conf_params;
extern char             *http_signal;

extern int              http_process_slot;
extern http_channel_t   http_channel;
extern int              http_last_process;
extern http_process_t   http_processes[HTTP_MAX_PROCESSES];

extern unsigned int     http_process;
extern pid_t            http_pid;
extern pid_t            http_new_binary;
extern unsigned int     http_inherited;
extern unsigned int     http_daemonized;
extern unsigned int     http_threaded;
extern unsigned int     http_exiting;

extern pid_t            new_binary;

extern sig_atomic_t     http_reap;
extern sig_atomic_t     http_sigio;
extern sig_atomic_t     http_sigalrm;
extern sig_atomic_t     http_quit;
extern sig_atomic_t     http_debug_quit;
extern sig_atomic_t     http_terminate;
extern sig_atomic_t     http_noaccept;
extern sig_atomic_t     http_reconfigure;
extern sig_atomic_t     http_reopen;
extern sig_atomic_t     http_change_binary;

extern int              http_channel_id;
extern const char       *the_db_name;

#include "processlog.h"
#include "getoption.h"

#include "standard/nstring.h"
#include "standard/nfile.h"
#include "standard/CThrowException.h"

#include "snetwork/SignalProcess.h"
#include "snetwork/MasterProcess.h"
#include "snetwork/Sock.h"

void readFile();
std::string get_config(std::string modulename,std::string param);
int ProcessDameon();
void ProcessCycle();
int initrun();
int endrun();
int mainrun(int argc, char **argv, const char *dbname);

